package top.january.test;

import ohos.ace.ability.AceInternalAbility;
import ohos.app.AbilityContext;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.MessageOption;
import ohos.rpc.MessageParcel;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.Map;

public class MaininternalAbility extends AceInternalAbility {
    private static final String BUNDLE_NAME = "top.january.test";
    private static final String ABILITY_NAME = "top.january.test.MaininternalAbility";
    private static final int SUCCESS = 0;
    private static final String ERROR = "error";

    static final int ACTION_MUTE_SWITCH = 1000;
    static final int ACTION_MUTE_STATE = 1001;
    static final int ACTION_SET_VOLUME = 1002;
    static final int ACTION_GET_VOLUME = 1003;


    MainAbility m_pAbility;
    // 定义日志标签
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "JSApp");

    private static MaininternalAbility instance;
    private AbilityContext abilityContext;

    // 如果多个Ability实例都需要注册当前InternalAbility实例，需要更改构造函数，设定自己的bundleName和abilityName
    public MaininternalAbility() {
        super(BUNDLE_NAME, ABILITY_NAME);
    }

    public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
        String dataStr = data.readString();
        HiLog.debug(LABEL_LOG, "get request: code %{public}d, param: %{public}s", code, dataStr);
        Map<String, Object> result = new HashMap<String, Object>();
        boolean isSuccessed = true;
        switch (code) {
            case ACTION_MUTE_STATE: {
                // 返回结果当前仅支持String，对于复杂结构可以序列化为ZSON字符串上报
                result.put("code", m_pAbility.getMuteState());
                // SYNC
                break;
            }
            case ACTION_MUTE_SWITCH: {
                m_pAbility.switch_mute();
                result.put("code", m_pAbility.getMuteState());
                break;
            }
            case ACTION_GET_VOLUME: {
                ZSONObject zsonObject = ZSONObject.stringToZSON(dataStr);
                String stream = zsonObject.getString("stream");
                result.put("code", m_pAbility.getVolume(stream));
                break;
            }
            case ACTION_SET_VOLUME: {
                ZSONObject zsonObject = ZSONObject.stringToZSON(dataStr);
                String stream = zsonObject.getString("stream");
                int volume = zsonObject.getIntValue("volume");
                m_pAbility.setVolume(stream, volume);
                result.put("code", m_pAbility.getVolume(stream));
                break;
            }
            default: {
                result.put("code", ERROR);
                isSuccessed = false;
            }
        }
        if (option.getFlags() == MessageOption.TF_SYNC) {
            HiLog.debug(LABEL_LOG, "send back result");
            reply.writeString(ZSONObject.toZSONString(result));
        }
        return true;
    }

    /**
     * Internal ability 注册接口。
     */
    public static void register(AbilityContext abilityContext) {

        instance = new MaininternalAbility();
        instance.onRegister(abilityContext);
    }

    private void onRegister(AbilityContext abilityContext) {
        m_pAbility = (MainAbility)abilityContext;
        this.abilityContext = abilityContext;
        this.setInternalAbilityHandler((code, data, reply, option) -> {
            return this.onRemoteRequest(code, data, reply, option);
        });
    }

    /**
     * Internal ability 注销接口。
     */
    public static void unregister() {
        instance.onUnregister();
    }

    private void onUnregister() {
        abilityContext = null;
        this.setInternalAbilityHandler(null);
    }


}